<?php
/**
 * 网站地图XML生成
 * @author cjli
 *
 */
class SitemapAction extends AdminAction
{
	public function map()
	{
		$config = SiteModel::getShopConfigList();
		if($_POST['dosubmit']){
			//配置保存
			$config2 = array(
        'homepage_changefreq' => $_POST['homepage_changefreq'],
        'homepage_priority' => $_POST['homepage_priority'],
        'category_changefreq' => $_POST['category_changefreq'],
        'category_priority' => $_POST['category_priority'],
        'content_changefreq' => $_POST['content_changefreq'],
        'content_priority' => $_POST['content_priority'],
        );
        SiteModel::updateShopConfig($config2);
			
			$language_id = C('language_id');
			
			//加载地图类
			import('COM.GoogleSitemap');
			$domain = getHost();
			$today = date('Y-m-d');
			$sm     =& new google_sitemap();
			
			//首页
	    $smi    =& new google_sitemap_item($domain, $today, $_POST['homepage_changefreq'], $_POST['homepage_priority']);
	    $sm->add_item($smi);
	    
	    /* 商品分类 */
			$categoryList = CategoryModel::getCategoryListBase(CategoryModel::TYPE_CATEGORY);
			foreach ($categoryList as $cat){
				$smi    =& new google_sitemap_item($domain.'/product/category/catid/'.$cat['id'], $today, $_POST['category_changefreq'], $_POST['category_priority']);
   			$sm->add_item($smi);
			}
			
			//商品列表
			$product_db = D('product');
			$product_db->field('id');
			$product_db->where(array('display' => 1));
//			$product_db->field('id,product_items.name');
//			$product_db->join('product_items AS product_items.product_id = product.id');
//			$product_db->where(array('display' => 1, 'language_id' => $language_id));
			$productList = $product_db->select();
			foreach ($productList as $product){
				$smi    =& new google_sitemap_item($domain.'/product/info/id/'.$product['id'], $today, $_POST['content_changefreq'], $_POST['content_priority']);
   			$sm->add_item($smi);
			}
			
			
			
			//生成XML
			$sm_file = ROOT_PATH.'/sitemaps.xml';
	    if ($sm->build($sm_file))
	    {
	    	$this->success('操作成功！');
	    }
	    else
	    {
        $sm_file = DATA_PATH . 'sitemaps.xml';
        if ($sm->build($sm_file))
        {
            $this->success('操作成功！');
        }
        else
        {
           $this->error('操作失败！');
        }
	    }
		}else{
			
			$this->assign('priority',   array(1,0.9,0.8,0.7,0.6,0.5,0.4,0.3,0.2,0.1));
			$changefreq = array(
				'always' => '一直更新',
				'hourly' => '小时',
				'daily' => '天',
				'weekly' => '周',
				'monthly' => '月',
				'yearly' => '年',
				'never' => '从不更新',
			);
			$this->assign('changefreq', $changefreq);
			$this->assign('config', $config);
			$this->display('sitemap');
		}
	}
}